<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="net.groupme.model.Group"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.User"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.Collection"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<!-- meta tags, link to css:  -->
	<%@ include file="mymetaheader.jsp" %>
	
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Manage Contributor to group</title>

	<script type='text/javascript' src='<%= request.getContextPath()%>/dwr/interface/DWRUserManager.js'></script>
	<script type='text/javascript' src='<%= request.getContextPath()%>/dwr/engine.js'></script>
    <script type='text/javascript' src='<%= request.getContextPath()%>/dwr/util.js'></script>
	<script type="text/javascript" src="<%= request.getContextPath()%>/highslide/highslide.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath()%>/scriptaculous/scriptaculous.js"></script>


<script type="text/javascript" language="javascript"><!--

	<% Group groupWhereToAddContributor = null;
	if(request.getAttribute(VIEW_VOCABULARY.PARAMETER_ID)!=null){
		try{
			groupWhereToAddContributor = ObjectFactory.getGroup(request.getAttribute(VIEW_VOCABULARY.PARAMETER_ID).toString());
		}catch(Exception e){}
	}%>
	/**
	 * Remove a contributor.
	 */
	function removeContributor(username) {
		if(username != null && username != ""){
			dwr.util.setValue('status-message', "");
			DWRUserManager.removeContributor(username, "<%= groupWhereToAddContributor.getId() %>", removeContributorCallback);
		}
	}
	
	/**
	 * Update list of contributors after removing a new contributor.
	 */
	function removeContributorCallback(contributors){
		if(contributors.length == 0){
			dwr.util.setValue('contributorList', "<i>no contributors at the moment</i>", { escapeHtml:false });
		}else{
			var contributorString = "<ul>"; 
			for(i=0; i<contributors.length;i++){
				contributorString += "<li><a target=\"new\" href=\"<%= VIEW_VOCABULARY.RESTful_USER_URI_PATTERN %>" + contributors[i].username + "\">" + contributors[i].firstName + " " + contributors[i].lastName + "</a> (" + contributors[i].username +") " + 
				"<a href=\"#\" onclick=\"removeContributor('" + contributors[i].username + "')\" title=\"remove " + contributors[i].username + " as contributor\">[x]</a></li>";
			}
			contributorString += "</ul>";
			dwr.util.setValue('contributorList', contributorString, { escapeHtml:false });
		}
	}
	
	
	/**
	 * Add a contributor.
	 */
	function addContributor() {
		var username = $('user-selection').value;
		if(username != null && username != ""){
			dwr.util.setValue('status-message', "");
			DWRUserManager.addContributor(username, "<%= groupWhereToAddContributor.getId() %>", addContributorCallback);
		}else{
			dwr.util.setValue('status-message', "<strong style=\"color: gray;\">Please select a user from the <i>matching users</i> who should be added as contributor.</strong>", { escapeHtml:false });
		}
	}
	/**
	 * Update list of contributors after adding a new contributor.
	 */
	function addContributorCallback(contributors){
		if(contributors.length == 0){
			dwr.util.setValue('status-message', "<strong style=\"color: red;\">Problem while adding the contributor. Contributor was not added.</strong>", { escapeHtml:false });
		}else{
			var contributorString = "<ul>"; 
			for(i=0; i<contributors.length;i++){
				contributorString += "<li><a target=\"new\" href=\"<%= VIEW_VOCABULARY.RESTful_USER_URI_PATTERN %>" + contributors[i].username + "\">" + contributors[i].firstName + " " + contributors[i].lastName + "</a> (" + contributors[i].username +") " + 
				"<a href=\"#\" onclick=\"removeContributor('" + contributors[i].username + "')\" title=\"remove " + contributors[i].username + " as contributor\">[x]</a></li>";
			}
			contributorString += "</ul>";
			dwr.util.setValue('contributorList', contributorString, { escapeHtml:false });
		}
	}
	
	/**
	 * Search for users that match the given pattern.
	 */
	function search(){
		var query = $('query').value;
		dwr.util.setValue('status-message', "");
		if(query.length > 1){
			DWRUserManager.searchUsers(query, searchCallback);
		}else{
			dwr.util.setValue('status-message', "<strong style=\"color: gray;\">Please type in at least two letters to search for GroupMe! users you want to add as contributors.</strong>", { escapeHtml:false });
		}
	}
	/**
	 * Add the search results (users) into a select-form
	 */
	function searchCallback(data){
		var results = "no user found";
		if(data.length > 0){
			var results = "<strong>Matching users:</strong><br/><table><tr><td><select id=\"user-selection\" size=\"5\">";
			for(i=0; i < data.length; i++){
				results += "<option value=\"" + data[i].username + "\">" + data[i].firstName + " " + data[i].lastName + " (" + data[i].username + ")</option>";
			}
			results +="</select></td><td><input onclick=\"addContributor()\" type=\"button\" value=\"add user\"/></td></tr></table>";
			dwr.util.setValue('searchresults', results, { escapeHtml:false });
		}else{
			dwr.util.setValue('status-message', "<strong style=\"color: gray;\">No user found!</strong>", { escapeHtml:false });
		}
		
	}
	//-->
</script>
</head>
<body>
<!-- title image, logo:  -->
	<div id="groupme_smallresource_header">
		<img height="40px" src="<%= request.getContextPath() %>/images/groupme-logo-small.png" alt="Semantic Web"/>
	</div>
	
	<!-- Menu: -->
	<div id="small_menu">
	</div>
	<!-- content and sidebar -->
	<!-- Content: -->
	<div id="smallresource_content">
	<% 
	User owner = userManager.getUser();
	boolean reallyTheOwner = false;
	if(owner != null){
		for(User realOwner: groupWhereToAddContributor.getUsers(MODEL_CONSTANTS.ROLE_OWNER)){
			if(realOwner.getId().equals(owner.getId())){
				reallyTheOwner = true;
			}
		}
	}
	if(groupWhereToAddContributor != null && reallyTheOwner){%>
		<h3>Manage contributors</h3>
		<p><i>Contributors</i> are GroupMe! users, who are allowed to modify your group (<a href="<%= groupWhereToAddContributor.getURI() %>" target="new"><%= groupWhereToAddContributor.getName() %></a>). They are allowed to add, remove, or rearrange resources, but cannot delete your group.<br/><br/></p>
				
		<% 
		Collection<User> contributors = groupWhereToAddContributor.getUsers(MODEL_CONSTANTS.ROLE_EDITOR);
		String contributorString = null;
		for(User contributor: contributors){
			if(contributorString == null){
				contributorString = "<ul>"; 
			}
			contributorString += "<li><a target=\"new\" href=\"" + contributor.getURI() + "\">" + contributor.getFirstName() + " " + contributor.getLastName() + "</a> (" + contributor.getUserName() +") " + 
				"<a href=\"#\" onclick=\"removeContributor('" + contributor.getUserName() + "')\" title=\"remove " + contributor.getUserName() + " as contributor\">[x]</a> </li>";
		}
		if(contributorString == null){
			contributorString = "<i>no contributors at the moment</i>";
		}else{
			contributorString += "</ul>";
		}
		%>
		<p><strong>Group:</strong> <a href="<%= groupWhereToAddContributor.getURI() %>" target="new"><%= groupWhereToAddContributor.getName() %></a></p>
		<p><strong>Contributors:</strong></p> 
			<span id="contributorList"><%=contributorString %></span>
		
		<p><br/></p>
		<p><strong>Search for contributors:</strong></p>
		
		<table>
			<tr><td><input type="text" id="query" name="newContributor_parameter" size="20" /></td> <td><input onclick="search()" type="button" value="search"/></td> </tr>
		</table>
		
		<span id="searchresults"></span>
		<span id="status-message"></span>
		<br/><br/><br/><br/><br/><br/>
	<% } %>
	</div>
</body>
</html>